<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/25
 * Time: 9:55
 */

namespace App\Http\Controllers\System;


use App\Http\Controllers\Controller;

class CustomerController extends Controller
{
    const FILE_MAX_SIZE = 2 * 1024 * 1024;

    public function __construct()
    {
        $this->middleware('check.login');
    }

    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if (!$menu_id) {
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.customer.list', compact('menu_id', 'toolBarButton'));
    }

    public function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if (!$params['curr_menu_id']) {
            return response()->json(['code' => 400, '页面无菜单ID']);
        }
        $org_id = request()->get('id');
        try {
            $list = callApiServer('System.CustomerService.getInfoList', ['org_id' => $org_id]);
            return response()->json($list);
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 400, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }


    public function showAddPage()
    {
        return view('system.customer.new');
    }

    public function showEditPage()
    {
        $params['id'] = request('id');

        try {
            $info = callApiServer("System.CustomerService.getInfoById", $params);
            if (empty($info)) {
                return response()->json('对应数据不存在');
            }
            return view('system.customer.new', compact("info"));
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 500, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }

    function newcustomer()
    {
        return view('system.customer.addcustomer');
    }

    function linkManList()
    {

    }

    function customerSales()
    {

    }

    public function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['phone'] = request('tel');
        $params['taxid'] = request('taxid');
        $params['adds'] = request('adds');
        $params['province_id'] = request('province_id');
        $params['city_id'] = request('city_id');
        $params['district_id'] = request('district_id');
        $params['area_info'] = request('area_info');
        $params['address_detail'] = request('address_detail');
        $params['business_state'] = request('business_state');
        $params['business_type'] = request('business_type');
        $params['address_detail'] = request('address_detail');
        $params['bank'] = request('bank');
        $params['bankAcc'] = request('bankAcc');
        $params['is_threeInOne'] = request('is_threeInOne') == 'on' ? 1 : 0;
        $params['is_active'] = request('is_active') == 'on' ? 1 : 0;
        $params['description'] = request('description');

        if (!empty($_FILES['three_licence_elc'])) { //不为空才上传
            $file = $_FILES['three_licence_elc'];
            list($result, $goods_image, $msg) = $this->upload($file, 'cert');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['three_licence_elc'] = $goods_image;
        }

        if (!empty($_FILES['business_licence_number_elc'])) { //不为空才上传
            $file = $_FILES['business_licence_number_elc'];
            list($result, $goods_image, $msg) = $this->upload($file, 'cert');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['business_licence_number_elc'] = $goods_image;
        }

        if (!empty($_FILES['organization_code_electronic'])) { //不为空才上传
            $file = $_FILES['organization_code_electronic'];
            list($result, $goods_image, $msg) = $this->upload($file, 'cert');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['organization_code_electronic'] = $goods_image;
        }

        if (!empty($_FILES['tax_registration_certif_elc'])) { //不为空才上传
            $file = $_FILES['tax_registration_certif_elc'];
            list($result, $goods_image, $msg) = $this->upload($file, 'cert');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['tax_registration_certif_elc'] = $goods_image;
        }

        try {
            $rs = callApiServer("System.CustomerService.saveData", $params);
            if ($rs) {
                return response()->json(['code' => 200]);
            }
            return response()->json(['code' => 400, 'data' => $rs]);
        } catch (\Exception $e) {
            return response()->json(['code' => 400, 'data' => $e->getMessage()]);
        }

    }

    private function upload($file, $type)
    {
        if ($file['error'] != 0) return [false, '', '上传失败'];
        if ($file['size'] > self::FILE_MAX_SIZE) return [false, '', '文件尺寸超过'.self::FILE_MAX_SIZE/(1024*1024).'m'];
        if (!getimagesize($file['tmp_name'])) return [false, '', '请上传正确格式的图片'];
        $name = md5($file['tmp_name'] . time());
        $ext = explode('/', $file['type'])[1];
        $dest = env('RESOURCE_PATH') . '/' . $type . '/' . date('ymd');
        if (!is_dir($dest)) {
            mkdir($dest, 0777, true);
            chmod($dest, 0777);
        }
        $r = move_uploaded_file($file['tmp_name'], $dest . '/' . $name . '.' . $ext);
        if ($r) {
            return [true, '/' . $type . '/' . date('ymd') . '/' . $name . '.' . $ext, 'success'];
        } else {
            return [false, '', '上传失败'];
        }
    }

}